package org.bigplayer.utils;

import org.apache.commons.lang3.time.FastDateFormat;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期辅助类
 *
 * @cmopany: UMPAY
 * @Description:
 * @author: JL
 * @date: Aug 15, 2009 4:07:24 PM
 * @version V1.0
 */
public class DateUtil {
	public static final long DAY_MILLI = 24 * 60 * 60 * 1000; // 一天的MilliSecond
	/**
	 * 要用到的DATE Format的定义
	 */
	public static String DATE_FORMAT_DATEONLY = "yyyy-MM-dd"; // 年/月/日
	public static String DATE_FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss"; // 年/月/日
	//	public static SimpleDateFormat sdfDateTime = new SimpleDateFormat(DateUtil.DATE_FORMAT_DATETIME);
	//线程安全日期工具
	public static FastDateFormat sdfDateTime = FastDateFormat.getInstance(DateUtil.DATE_FORMAT_DATETIME);

	public static FastDateFormat sdfDateOnly = FastDateFormat.getInstance(DateUtil.DATE_FORMAT_DATEONLY);

	public static final FastDateFormat SHORTDATEFORMAT = FastDateFormat.getInstance("yyyyMMdd");

	public static final FastDateFormat TIME_FORMAT = FastDateFormat.getInstance("HHmmss");

	public static final FastDateFormat SHORT_DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");

	public static final FastDateFormat SHORT_MONTH_FORMAT = FastDateFormat.getInstance("yyyy-MM");

	public static final FastDateFormat LONG_DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

	public static final FastDateFormat HMS_FORMAT = FastDateFormat.getInstance("HH:mm:ss");

	public static final FastDateFormat formatTimestamp = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

	public static final String DATE_TIME = new String("yyMMddHHmmss");
	public static final String FULL_DATE_TIME = new String("yyyyMMddHHmmss");
	public static final String SHORT_TIME = new String("HH:mm");
	public static final String SHORT_TIME_HHMM = new String("HHmm");
	public static final FastDateFormat ROLL_DATE = FastDateFormat.getInstance("yyyyMMdd");


	public static final FastDateFormat SHORT_DATE_TIME = FastDateFormat.getInstance("yyMMddHHmm");

	public static final FastDateFormat FULL_DATE = FastDateFormat.getInstance("yyyyMMddHHmmss");


	/**
	 * 获得当前的日期毫秒
	 *
	 * @return
	 */
	public static long nowTimeMillis() {
		return System.currentTimeMillis();
	}

	/**
	 * 获得当前的时间戳
	 *
	 * @return
	 */
	public static Timestamp nowTimeStamp() {
		return new Timestamp(nowTimeMillis());
	}

	/**
	 * yyyyMMdd 当前日期
	 *
	 */
	public synchronized static String getReqDate() {
		return SHORTDATEFORMAT.format(new Date());
	}

	public static String timestamp2date(Timestamp timestamp) {
		return SHORTDATEFORMAT.format(timestamp);
	}

	public static String timestamp2shortdatetime(Timestamp timestamp) {
		return SHORT_DATE_TIME.format(timestamp);
	}

	public static String timestamp2time(Timestamp timestamp) {
		return TIME_FORMAT.format(timestamp);
	}

	/**
	 * HHmmss 当前时间
	 *
	 */
	public static String getCurrrentReqTime() {
		return TIME_FORMAT.format(new Date());
	}

	public synchronized static String date8(Date date) {
		return new SimpleDateFormat("yyyyMMdd").format(date);
	}

	public static String time6(Date date) {
		return new SimpleDateFormat("HHmmss").format(date);
	}

	/**
	 * yyyy-MM-dd 传入日期
	 *
	 * @param date
	 * @return
	 */
	public static String getReqDate(Date date) {
		return SHORT_DATE_FORMAT.format(date);
	}

	public static String TimestampToDateStr(Timestamp tmp) {
		return SHORT_DATE_FORMAT.format(tmp);
	}

	public static String getShortMonthStr(Timestamp tmp) {
		return SHORT_MONTH_FORMAT.format(tmp);
	}

	public static String getReqTime() {
		return HMS_FORMAT.format(new Date());
	}

	public static String getTimeStampStr(Date date) {
		return LONG_DATE_FORMAT.format(date);
	}

	public static String getLongDateStr() {
		return LONG_DATE_FORMAT.format(new Date());
	}

	public static String getLongDateStr(Timestamp time) {
		return LONG_DATE_FORMAT.format(time);
	}

	public static String getShortDateStr(Date date) {
		return SHORT_DATE_FORMAT.format(date);
	}

	public static String getShortDateStr() {
		return SHORT_DATE_FORMAT.format(new Date());
	}

	public static Date addMinute(Date date, int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, minute);
		return calendar.getTime();
	}

	public static Timestamp addMinute(Timestamp time, int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		calendar.add(Calendar.MINUTE, minute);
		return Timestamp.valueOf(sdfDateTime.format(calendar.getTime()));
	}

	/**
	 * 计算 hour 小时后的时间
	 */
	public static Date addHour(Date date, int hour) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR, hour);
		return calendar.getTime();
	}

	/**
	 * 得到day的起始时间点。
	 */
	public static Date getDayStart(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 得到day的终止时间点.
	 */
	public static Date getDayEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MILLISECOND, -1);
		return calendar.getTime();
	}

	/**
	 * 计算 day 天后的时间
	 */
	public synchronized static Date addDay(Date date, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, day);
		return calendar.getTime();
	}

	/**
	 * description : 计算 month 月后的时间
	 */
	public static Date addMonth(Date date, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, month);
		return calendar.getTime();
	}

	/**
	 * 得到month的起始时间点
	 */
	public static Date getMonthStart(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 得到month的终止时间点.
	 */
	public static Date getMonthEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.MILLISECOND, -1);
		return calendar.getTime();
	}

	public static Date addYear(Date date, int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, 365 * year);
		return calendar.getTime();
	}

	public static Timestamp strToTimestamp(String dateStr) {
		return Timestamp.valueOf(dateStr);
	}

	public static Timestamp strToTimestamp(Date date) {
		return Timestamp.valueOf(formatTimestamp.format(date));
	}

	public static Timestamp getCurTimestamp() {
		return Timestamp.valueOf(formatTimestamp.format(new Date()));
	}

	/**
	 * 取得两个日期之间的日数
	 *
	 * @return t1到t2间的日数，如果t2 在 t1之后，返回正数，否则返回负数
	 */
	public static long daysBetween(Timestamp t1, Timestamp t2) {
		return (t2.getTime() - t1.getTime()) / DAY_MILLI;
	}

	/**
	 * 取得两个日期之间的秒,分,时,日等...,根据传入的参数mms(单位:毫秒)来确定
	 */
	public static float anyBetween(Timestamp t1, Timestamp t2, float mms) {
		return (t2.getTime() - t1.getTime()) / mms;
	}

	/**
	 * 返回java.sql.Timestamp型的SYSDATE
	 */
	public static Timestamp getSysDateTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}

	/**
	 * 利用缺省的Date格式(YYYY/MM/DD)转换String到java.sql.Timestamp
	 */
	public static Timestamp toSqlTimestamp(String sDate) {
		if (sDate == null) {
			return null;
		}
		if (sDate.length() != DateUtil.DATE_FORMAT_DATEONLY.length()) {
			return null;
		}
		return toSqlTimestamp(sDate, DateUtil.DATE_FORMAT_DATEONLY);
	}

	/**
	 * 利用缺省的Date格式(YYYY/MM/DD hh:mm:ss)转化String到java.sql.Timestamp
	 *
	 */
	public static Timestamp toSqlTimestamp(String sDate, String sFmt) {
		String temp = null;
		System.out.println("sDate = " + sDate);
		System.out.println("sFmt  = " + sFmt);
		if (sDate == null || sFmt == null) {
			return null;
		}
		if (sDate.length() != sFmt.length()) {
			return null;
		}
		if (sFmt.equals(DateUtil.DATE_FORMAT_DATETIME)) {
			temp = sDate.replace('/', '-');
			temp = temp + ".000000000";
		} else if (sFmt.equals(DateUtil.DATE_FORMAT_DATEONLY)) {
			temp = sDate.replace('/', '-');
			temp = temp + " 00:00:00.000000000";
		} else {
			return null;
		}
		System.out.println("Temp = " + temp);
		return Timestamp.valueOf(temp);
	}

	/**
	 * 以YYYY/MM/DD HH24:MI:SS格式返回系统日期时间
	 */
	public static String getSysDateTimeString() {
		return toString(new Date(System.currentTimeMillis()), DateUtil.sdfDateTime);
	}

	/**
	 * 根据指定的Format转化java.util.Date到String
	 */
	public static String toString(Date dt, String sFmt) {
		// add by SJNS/zq 03/16
		if (dt == null) {
			return "";
		}
		if (sFmt.equals(DateUtil.DATE_FORMAT_DATETIME)) { // "YYYY/MM/DD
			// HH24:MI:SS"
			return toString(dt, DateUtil.sdfDateTime);
		} else { // Default , YYYY/MM/DD
			return toString(dt, DateUtil.sdfDateOnly);
		}
	}

	/**
	 * java.util.Date到String
	 */
	public static String toString(Date dt, FastDateFormat formatter) {
		String sRet = null;

		try {
			sRet = formatter.format(dt).toString();
		} catch (Exception e) {
			e.printStackTrace();
			sRet = null;
		}

		return sRet;
	}


	/**
	 * 转换java.sql.Timestamp到String，格式为YYYY-MM-DD HH24:MI
	 */
	public static String toSqlTimestampString2(Timestamp dt) {
		if (dt == null) {
			return null;
		}
		String temp = toSqlTimestampString(dt, DateUtil.DATE_FORMAT_DATETIME);
		return temp.substring(0, 16);
	}

	public static String toString(Timestamp dt) {
		return dt == null ? "" : toSqlTimestampString2(dt);
	}

	/**
	 * 根据指定的格式转换java.sql.Timestamp到String
	 */
	public static String toSqlTimestampString(Timestamp dt, String sFmt) {
		String temp = null;
		String out = null;
		if (dt == null || sFmt == null) {
			return null;
		}
		temp = dt.toString();
		if (sFmt.equals(DateUtil.DATE_FORMAT_DATETIME) || // "YYYY/MM/DD
				// HH24:MI:SS"
				sFmt.equals(DateUtil.DATE_FORMAT_DATEONLY)) { // YYYY/MM/DD
			temp = temp.substring(0, sFmt.length());
			out = temp.replace('/', '-');
		}
		return out;
	}

	// 得到当前日期的星期
	public static int getWeek() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int w = cal.get(Calendar.DAY_OF_WEEK);
		return w;
	}

	// 得到当前日期时间
	// 当前时间日期格式:yyMMddHHmmss
	public static String getDateAndTime() {
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_TIME);
		return sdf.format(new Date());
	}

	// 得到当前日期时间
	// 当前日期时间格式:yyyyMMddHHmmss
	public static String getFullDateAndTime() {
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.FULL_DATE_TIME);
		return sdf.format(new Date());
	}

	/**
	 * 得到今天回滚n天的日期 回滚n天日期时间格式:yyyy-MM-dd if amount > 0 日期>今天日期 if amount < 0
	 * 日期<今天日期
	 *
	 * @param amount
	 * @param sf
	 * @return
	 */
	public static String getRollCurrentDate(int amount, String sf) {
		Calendar date = Calendar.getInstance();
		date.add(Calendar.DAY_OF_MONTH, amount);
		return formatDateForMore(date.getTime(), sf);
	}

	/**
	 * 得到今天回滚n天的日期 回滚n天日期时间格式:yyyy-MM-dd if amount > 0 日期>今天日期 if amount < 0
	 * 日期<今天日期
	 *
	 * @param date
	 * @param amount
	 * @param sf
	 * @return
	 */
	public static String getRollCurrentDate(Date date, int amount, String sf) {
		Date currentDate = date;
		Calendar _date = Calendar.getInstance();
		if (currentDate != null) {
			_date.setTime(currentDate);
		}
		_date.add(Calendar.DAY_OF_MONTH, amount);
		return formatDateForMore(_date.getTime(), sf);
	}

	/**
	 * 取得当前时间的前几天或者后几天
	 *
	 * @param date
	 * @param amount
	 * @return
	 */
	public static Date getRollCurrentDate(Date date, int amount) {
		Calendar current = Calendar.getInstance();
		current.setTime(date);
		current.add(Calendar.DAY_OF_MONTH, amount);
		return current.getTime();
	}

	/**
	 * 格式化日期
	 *
	 * @param currentDate
	 * @param sf
	 * @return
	 */
	public static String formatDateForMore(Date currentDate, String sf) {
		String formatDate = "";
		if (SHORTDATEFORMAT.getPattern().equals(sf)) {
			formatDate = SHORTDATEFORMAT.format(currentDate.getTime());
		} else if (SHORT_DATE_FORMAT.getPattern().equals(sf)) {
			formatDate = SHORT_DATE_FORMAT.format(currentDate.getTime());
		} else if (LONG_DATE_FORMAT.getPattern().equals(sf)) {
			formatDate = LONG_DATE_FORMAT.format(currentDate.getTime());
		} else if (formatTimestamp.getPattern().equals(sf)) {
			formatDate = formatTimestamp.format(currentDate.getTime());
		}
		return formatDate;
	}

	/**
	 * 取得当前日期
	 *
	 * @return
	 */
	public static Date getCurrentDate() {
		Calendar date = Calendar.getInstance();
		return date.getTime();
	}



	public static String formatDateForMore(Timestamp timestamp, FastDateFormat sf) {
		return sf.format(timestamp);
	}

	/**
	 * 时间格式:HH:MM
	 */
	public static String getShortTime() {
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.SHORT_TIME);
		return sdf.format(new Date());
	}

	/**
	 * 时间格式:HHMM
	 */
	public static Date getShortTime(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.SHORT_TIME_HHMM);
		try {
			return sdf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 时间格式:HH:MM
	 */
	public static String getShortTimeStr(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.SHORT_TIME);
		String retTime = sdf.format(getShortTime(time));
		return retTime;
	}

	/**
	 * 时间格式:HHMM
	 */
	public static String getHHMMTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.SHORT_TIME_HHMM);
		return sdf.format(date);
	}


	// 日期格式: yyyMMdd
	public synchronized static String getDate(Date date) {
		return SHORTDATEFORMAT.format(date);
	}

	// 日期格式: yyyyMMddHHmmss
	public static String getFullDateAndTime(Timestamp tmp) {
		return FULL_DATE.format(tmp);
	}


	/**
	 * 比较两个日期的大小
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static boolean compareDate(String startDate, String endDate) {
		try {
			Date start = SHORT_DATE_FORMAT.parse(startDate);
			Date end = SHORT_DATE_FORMAT.parse(endDate);
			return compareDate(start, end);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar c = Calendar.getInstance();
		return compareDate(c.getTime(), c.getTime());
	}

	/**
	 * 比较两个日期的大小
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static boolean compareDate(Date startDate, Date endDate) {
		if (startDate.compareTo(endDate) >= 0) {
			return true;
		}
		return false;
	}


}